using UnityEngine;
using System.Collections;
using LogicalComponent.rules.grid;

[RequireComponent (typeof (Projector))]

public class Tile : MonoBehaviour, ITile {

    private TileLogic tileLogic;
    // Hacked until TileInterface is fixed for unity.
    public MoveCostModType MoveModType { get { return MoveCostModType.ADDER;} set{return;} }
    public int MoveModValue { get { return 0;} set{return;} }

    //the height where the pawn sits, it is a position in the game world and not relative to the tile
    //this is generated by raycasting at the surface of the underlying terrain
    //it should never be set manually
    //temporarily a public variable for testing purposes
    public float tileHeight;

    //the vertical distance under the tile that the tileObject is spawned
    public float tileObjectHeight;

    //setting this outside of the range of the tileObjects will prevent a default object from being spawned
    //in most cases a defaultObject will not be wanted, and the objects will be generated seperatly
    public int defaultObject = -1;


    public GameObject[] tileObjects;

    [ContextMenu("SetHeightScan")]
    void SetHeightScan ()
    {
        RaycastHit hit;
        int layerMask = 1 << 8;
        layerMask = ~layerMask;

        if (Physics.Raycast(transform.position, -Vector3.up, out hit, Mathf.Infinity, layerMask))
        {
            tileHeight = hit.point.y;
        }

    }

    // Use this for initialization
    void Start () {
        this.tileLogic = new TileLogic();
        this.tileLogic.MoveModValue = this.MoveModValue;
        this.tileLogic.MoveModType = this.MoveModType;

    }

    // Update is called once per frame
    void Update () {
    }

    public float GetTileHeight() {
        this.SetHeightScan();
        return tileHeight;
    }

    //do not use this with generated objects
    public void SetTileSize(float size) {
        Projector p = this.GetComponent<Projector>();
        p.orthographicSize = (float).5 * size;
    }

    public Vector3 GetPawnPosition(){
        Vector3 pos = transform.position;
        pos.y = this.GetTileHeight();
        return pos;
    }

    public void SpawnTileObject(int i){
        if(i >= tileObjects.Length || i < 0){
            Debug.Log ("Attempted to spawn tileObject out of range");
            return;
        }
        GameObject tileObject;
        tileObject = (GameObject)Instantiate(tileObjects[i], new Vector3(this.transform.position.x, this.transform.position.y - tileObjectHeight, this.transform.position.z), this.transform.rotation);
        tileObject.transform.parent = this.transform;
        this.SetHeightScan();
    }

    public void SpawnTileObject(int i, float newHeight){
        tileObjectHeight = newHeight;
        if(i >= tileObjects.Length || i < 0){
            Debug.Log ("Attempted to spawn tileObject out of range");
            return;
        }
        GameObject tileObject;
        tileObject = (GameObject)Instantiate(
            tileObjects[i], new Vector3(this.transform.position.x, this.transform.position.y -
            tileObjectHeight, this.transform.position.z), this.transform.rotation);
        tileObject.transform.parent = this.transform;
        this.SetHeightScan();
    }

    public void SpawnDefaultObject(){
        this.SpawnTileObject(defaultObject);
    }

    //destroys all the children
    public void DespawnTileObjects(){
        Transform[] children = this.gameObject.GetComponentsInChildren<Transform>();
        foreach(Transform child in children){
            DestroyImmediate(child.gameObject);
        }
    }
}
